package view.ui
{
	import control.manager.BitmapManager;
	import control.manager.NameManager;
	import control.util.GameFacade;
	import control.util.tooltip.ToolTip;
	
	import data.GameConst;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	
	import view.Immortal;
	import view.stage.StageLoading;
	
	public class UILogin extends Sprite
	{
		private const NAME :String = "loginUI";
		
		private const NORMAL :String = "normal";
		private const ONMOUSE :String = "onMouse";
		private const SELECT :String = "select";
		/**
		 * swf加载器 
		 */		
		private var uiLoader :Loader;
		/**
		 * 登录ui  
		 */		
		private var mc :MovieClip;
		/**
		 * 人物选择按钮列表 
		 */		
		private var btnPlayerList :Array;
		/**
		 * 随机名字按钮 
		 */		
		private var btnRandom :MovieClip;
		/**
		 * 开始游戏按钮 
		 */		
		private var btnStart :MovieClip
		/**
		 * 名字文本 
		 */		
		private var txtName :TextField;
		/**
		 * 鼠标上的当前对象 
		 */		
		private var curObj :MovieClip;
		/**
		 * 资源加载界面 
		 */		
		private var stageLoading :StageLoading;
		/**
		 * 需要传递的玩家信息，  
		 *   0     1     2
		 *  名字      性别     职业
		 */		
		private var playerDataAry :Array = [];
		
		private var tipsAry :Array = [["猛将哥","what the fuck"],["猛将姐","what the fuck"],["刺客哥","what the fuck"],["刺客姐","what the fuck"],["谋士哥","what the fuck"],["谋士姐","what the fuck"],];
		public function UILogin()
		{
			super();
			init();
		}
		
		/**
		 * 销毁 
		 * 
		 */		
		public function destroy():void
		{
			removeEventListener(MouseEvent.MOUSE_UP,mouseHandler);
			removeEventListener(MouseEvent.MOUSE_MOVE,mouseHandler);
			this.btnPlayerList = null;
			this.curObj = null;
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		public function show():void
		{
			
		}
		
		/**
		 * 初始化 
		 * 
		 */		
		private function init():void
		{
			this.uiLoader = new Loader();
			this.uiLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			this.uiLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			var url :String = GameConst.uiLoginURL;
			var context :LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			this.uiLoader.load(new URLRequest(url),context);
		}
		/**
		 *  加载swf完成 
		 * @param e
		 * 
		 */		
		private function completeHandler(e :Event):void
		{

			mc = BitmapManager.instance.getMCByName(NAME);
			if(!mc)
			{
				return;
			}
			addChild(mc);
			mc.x = (stage.stageWidth - mc.width )/2;
			mc.y = (stage.stageHeight - mc.height)/2;
			
			if(!this.btnPlayerList)
			{
				this.btnPlayerList = [];
				this.btnPlayerList.push(mc.btn1);
				this.btnPlayerList.push(mc.btn2);
				this.btnPlayerList.push(mc.btn3);
				this.btnPlayerList.push(mc.btn4);
				this.btnPlayerList.push(mc.btn5);
				this.btnPlayerList.push(mc.btn6);
			}
			mc.btn5.gotoAndStop(SELECT);
			this.btnRandom = mc.btnRandom;
			this.btnStart = mc.btnStart;
			this.txtName = mc.txtName;
			this.txtName.text = NameManager.creatXingMing();
			setMouseMode();
			
			addEventListener(MouseEvent.MOUSE_UP,mouseHandler);
			addEventListener(MouseEvent.MOUSE_MOVE,mouseHandler);
			
			this.uiLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			this.uiLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			this.uiLoader.unloadAndStop(true);
			this.uiLoader = null;
			
			//删除小loadingUI 
			var smallMC :MovieClip = Immortal.instance.smallLoading;
			if(smallMC)
			{
				smallMC.visible = false;
			}
			
			//开始加载资源
			stageLoading  = new StageLoading(/*Immortal.instance*/);
			stageLoading.hide();
//			Immortal.instance.setStage(stageLoading);
			GameFacade.I.game.setStage(stageLoading);
		}
		/**
		 * 鼠标操作 
		 * @param event
		 * 
		 */		
		private function mouseHandler(event :MouseEvent):void
		{
			switch(event.type)
			{
				case MouseEvent.MOUSE_UP:
					if(this.curObj)
					{
						if(this.curObj == this.btnRandom)
						{
							getRandomName();
						}else if(this.curObj == this.btnStart)
						{
							startGame();
						}else
						{
							trace(this.curObj);
							this.curObj.gotoAndStop(SELECT);
							unSelect();
						}
					}
					break;
				case MouseEvent.MOUSE_MOVE:
					onMouse(event);
					break;
			}
		}
		/**
		 * 获取一个随机名字
		 */		
		private function getRandomName():void
		{
			trace("获取随机姓名");
			this.txtName.text = NameManager.creatXingMing();
		}
		/**
		 *  点击开始游戏
		 */		
		private function startGame():void
		{
			trace("点击了创建人物界面的开始游戏按钮");
			this.visible = false;
			stageLoading.clickStart = true;
			this.playerDataAry.push(this.txtName.text);
			this.playerDataAry.push("1");   //1为男性  2为女性
			this.playerDataAry.push("3");   //1猛将       2刺客      3谋士
			stageLoading.show(playerDataAry);
			destroy();
		}
		/**
		 * 恢复其他人物mc的普通状态
		 */		
		private function unSelect():void
		{
			for each(var item :MovieClip in this.btnPlayerList)
			{
				if(item == this.curObj && this.curObj.currentFrameLabel == SELECT) continue;
				item.gotoAndStop(NORMAL);
			}
		}
		/**
		 * 设置所有响应鼠标事件MC的按钮模式
		 */		
		private function setMouseMode():void
		{
			for each(var item :MovieClip in this.btnPlayerList)
			{
				item.buttonMode = true;
			}
			this.btnRandom.buttonMode = true;
			this.btnStart.buttonMode = true;
		}
		/**
		 * 鼠标移动检测当前鼠标上的对象 
		 * @param e
		 * 
		 */		
		private function onMouse(e :MouseEvent):void
		{
			if(this.btnPlayerList && this.btnPlayerList.length > 0)
			{
				for each(var item :MovieClip in this.btnPlayerList)
				{
					
					if(item.hitTestPoint(e.stageX,e.stageY))
					{
						
						this.curObj = item;
//						trace(this.curObj.name);
						var index :int = int(this.curObj.name.substr(-1)) - 1;
						ToolTip.instance.show(this.curObj,this.tipsAry[index][0],this.tipsAry[index][1]);
						
						if(item.currentFrameLabel == SELECT) return;
						this.curObj.gotoAndStop(ONMOUSE);
					}else
					{
						if(item.currentFrameLabel == SELECT) continue;
						item.gotoAndStop(NORMAL);
					}
				}
			}
			
			
			if(this.btnRandom.hitTestPoint(e.stageX,e.stageY))
			{
				this.curObj = this.btnRandom;
			}
			
			if(this.btnStart.hitTestPoint(e.stageX,e.stageY))
			{
				this.curObj = this.btnStart;
			}
		}
		/**
		 * 加载swf出错 
		 * @param e
		 * 
		 */		
		private function errorHandler(e :IOErrorEvent):void
		{
			trace("加载登录界面出错");
			this.uiLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			this.uiLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
		}
		
		
	}
}